using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utility;

namespace MKDS_Course_Editor
{
    public partial class obji_edit : Form
    {
        public nkm.OBJi item = null;
        public obji_edit(nkm.OBJi i)
        {
            InitializeComponent();
            item = i;
        }

        private void nkm_edit_Load(object sender, EventArgs e)
        {
            //listView1.SelectedIndices.Clear();
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Text == BitConverter.ToString(item.ObjectID).Replace("-", ""))
                {
                    listView1.SelectedIndices.Add(i);
                    break;
                }
            }
            textBox1.Text = BitConverter.ToString(item.RouteID).Replace("-", "");
            textBox2.Text = BitConverter.ToString(item.Setting1).Replace("-", "");
            textBox3.Text = BitConverter.ToString(item.ObjectID).Replace("-", "");
            numericUpDown9.Value = Convert.ToDecimal(item.TransX);
            numericUpDown8.Value = Convert.ToDecimal(item.TransY);
            numericUpDown7.Value = Convert.ToDecimal(item.TransZ);
            numericUpDown6.Value = Convert.ToDecimal(item.RotX);
            numericUpDown5.Value = Convert.ToDecimal(item.RotY);
            numericUpDown4.Value = Convert.ToDecimal(item.RotZ);
            numericUpDown1.Value = Convert.ToDecimal(item.ScaleX);
            numericUpDown2.Value = Convert.ToDecimal(item.ScaleY);
            numericUpDown3.Value = Convert.ToDecimal(item.ScaleZ);
            if (BitConverter.ToString(item.Setting5).Replace("-", "") == "01000000")
            {
                checkBox1.Checked = true;
            }
        }
        byte[] stringtobyte(string a)
        {
            byte[] buffer;
            int discarded;
            buffer = HexEncoding.GetBytes(a, out discarded);
            return buffer;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            item.TransX = (float)numericUpDown9.Value;
            item.TransY = (float)numericUpDown8.Value;
            item.TransZ = (float)numericUpDown7.Value;
            item.RotX = (float)numericUpDown6.Value;
            item.RotY = (float)numericUpDown5.Value;
            item.RotZ = (float)numericUpDown4.Value;
            item.ScaleX = (float)numericUpDown1.Value;
            item.ScaleY = (float)numericUpDown2.Value;
            item.ScaleZ = (float)numericUpDown3.Value;
            item.ObjectID = stringtobyte(textBox3.Text);
            item.RouteID = stringtobyte(textBox1.Text);
            item.Setting1 = stringtobyte(textBox2.Text);
            /*if (listView1.SelectedIndices.Count != 0)
            {
                item.SubItems[10].Text = listView1.SelectedItems[0].Text + textBox1.Text;
            }
            else
            {
                item.SubItems[10].Text = item.SubItems[10].Text.Remove(4) + textBox1.Text;
            }*/
            if (checkBox1.Checked)
            {
                item.Setting5 = new byte[] { 0x01,0x00,0x00,0x00 };
            }
            else
            {
                item.Setting5 = new byte[] { 0x00, 0x00, 0x00, 0x00 };
            }
            MKDS_Course_Editor.nkm.item = item;
            this.Close();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                listView1.SelectedIndices.Clear();
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    if (listView1.Items[i].Text == textBox3.Text)
                    {
                        listView1.SelectedIndices.Add(i);
                        break;
                    }
                }
            }
            catch { }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                textBox3.Text = listView1.Items[listView1.SelectedIndices[0]].Text;
            }
            catch { }
        }
    }
}
